package GUIs;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GridLayout;
import javax.swing.*;

import MVC.*;


public class WorkerPanel extends JPanel implements Observer{

	private JTextField worker_text_1;
	//private JTextArea worke_area; 
	private JTextField worker_text_2;
	private JTextField worker_text_3;
	private JButton start_button;
	private DataObserver data_observer;
	/**
	 * Constructor to create an object  of WorkerPanel with data_observer and initial a set of valriable
	 * @param data_observer the object of Observer pattern 
	 */
	public WorkerPanel(DataObserver data){

		this.data_observer = data;
		data.registerObserver(this);
		
		this.setLayout(new GridLayout(4,1,0,10));
		this.add(addWorkerPanel_1());
		this.add(addWorkerPanel_2());
		this.add(addWorkerPanel_3());
		start_button = new JButton("Start");
		this.add(start_button);
		
		update();
	}
	/**
	 * Method to get "Start" Button
	 * @return "Start" Button
	 */
	public JButton getStartButton(){
		return start_button;
	}
	/**
	 * Method to get workerpanel for worker 1 
	 * @return workerpanel of worker 1 
	 */
	public JPanel addWorkerPanel_1(){
		JPanel worker_panel_1 = new JPanel();
		worker_panel_1.setLayout(new BorderLayout(5,5));
		worker_panel_1.add(new JLabel("Yanchao: "), BorderLayout.WEST);
		
		worker_text_1 = new JTextField();
		worker_text_1.setBackground(Color.WHITE);
		worker_text_1.setEditable(false);
		worker_panel_1.add(worker_text_1,BorderLayout.CENTER);
		
		return worker_panel_1;
	}

	/**
	 * Method to get worker panel for worker 3 
	 * @return worker panel of worker 3 
	 */
	public JPanel addWorkerPanel_2(){
		JPanel worker_panel_2 = new JPanel();
		worker_panel_2.setLayout(new BorderLayout(5,5));
		worker_panel_2.add(new JLabel("Madiha: "), BorderLayout.WEST);
		worker_text_2 = new JTextField();
		worker_text_2.setBackground(Color.WHITE);
		worker_text_2.setEditable(false);
		worker_panel_2.add(worker_text_2,BorderLayout.CENTER);
		
		return worker_panel_2;
	}

	/**
	 * Method to get workerpanel for worker 3 
	 * @return workerpanel of worker 3 
	 */
	public JPanel addWorkerPanel_3(){
		JPanel worker_panel_3 = new JPanel();
		worker_panel_3.setLayout(new BorderLayout(5,5));
		worker_panel_3.add(new JLabel("Mengting: "), BorderLayout.WEST);
		worker_text_3 = new JTextField();
		worker_text_3.setBackground(Color.WHITE);
		worker_text_3.setEditable(false);
		worker_panel_3.add(worker_text_3,BorderLayout.CENTER);
		
		return worker_panel_3;
	}
	/**
	 * Method to show and update to new receipt
	 */
	@Override
	public void update() {
		if(data_observer.getReceipt() != null){
		int index_last = data_observer.getReceipt().length()-1;
		String string_number = data_observer.getReceipt().substring(index_last);
		String string_recept = data_observer.getReceipt().substring(0,index_last-1);
		int worker_number = Integer.parseInt(string_number);	
		
		switch(worker_number){
		case 1:
		this.worker_text_1.setText(string_recept);	
			break;
		case 2:
			worker_text_2.setText(string_recept);	
			break;
		case 3:
			worker_text_3.setText(string_recept);	
			break;
		}
		}
	}
}
